# coding:UTF-8
import asyncio
import time


async def main():
    print("1")
    await asyncio.sleep(1)
    print("2")


async def say(delay, what):
    res = await asyncio.sleep(delay)
    print(res, what)
    return f"{delay} {what}"


async def main2():
    # 耗时 3秒
    print(f"started at {time.strftime('%X')}")
    await say(1, 'hello')
    await say(2, 'world')
    print(f"end at {time.strftime('%X')}")


async def main3():
    # 耗时 2秒
    print(f"started at {time.strftime('%X')}")
    task1 = asyncio.create_task(say(1, 'hello'))
    task2 = asyncio.create_task(say(2, 'world'))
    await task1
    await task2
    print(f"end at {time.strftime('%X')}")


async def main4():
    # 耗时 2秒
    print(f"started at {time.strftime('%X')}")
    task1 = asyncio.create_task(say(1, 'hello'))
    task2 = asyncio.create_task(say(2, 'world'))
    res = await asyncio.gather(task1, task2)
    print(f"end {res}")
    print(f"end at {time.strftime('%X')}")


async def main5():
    # 耗时 2秒
    print(f"started at {time.strftime('%X')}")
    res = await asyncio.gather(say(1, 'hello'), say(2, 'world'))
    print(f"end {res}")
    print(f"end at {time.strftime('%X')}")


if __name__ == '__main__':
    # res = asyncio.sleep(2)
    # print(res)

    # coro = main()
    # print(coro)
    # asyncio.run(coro)

    # asyncio.run(main2())
    # asyncio.run(main3())
    # asyncio.run(main4())
    asyncio.run(main5())
